$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var tianjian = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["tianjian"];
    var fujian = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["fujian"];
    var guangxi = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["guangxi"];
    var xinsanli = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["xinsanli"];
    var guangzhoumatou = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["guangzhoumatou"];
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeper.html";
    });

    var warehouseCode = [];
    if (tianjian) {
        warehouseCode.push("W0402011")
    }
    if (fujian) {
        warehouseCode.push("W0402005")
    }
    if (guangxi) {
        warehouseCode.push("W0402006")
    }
    if (xinsanli) {
        warehouseCode.push("W0402004")
    }
    if (guangzhoumatou) {
        warehouseCode.push("W0402010")
    }

    $("#goods_id").focus();

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "deliverWK.html";
    });

    //扫描发货单号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == "13") {
            var code = $(this).val();
            if (!code) return $alert("运单号不可为空");
            getSaleoutorders(code);
        }
    });

    //获取发货单列表
    getSaleoutorders("");

    function getSaleoutorders(code) {
        //清空页面中的发货单
        $("#work_list").find("li").remove();
        //加载提示符 显示
        $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            url: url + "/app/deliver/getWKSaleoutorders",
            type: "post",
            data: {
                code: code,
                warehouseCode: JSON.stringify(warehouseCode)
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    //清空页面中的发货单
                    $("#work_list").find("li").remove();
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    //console.log(data);
                    if (data.length == 0) return $alert("暂无要发货的运单");
                    var data_html = '';
                    for (var i = 0; i < data.length; i++) {
                        data_html += '<li>' +
                            '<p class="li_title">运单号：<span class="sp billNo">' + data[i].billNo + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                            '<div class="row no-gutter li_content"><div class="col-100">' +
                            '<p class="p_none"><span>id:</span><span class="sp goods_id">' + data[i]._id + '</span></p>' +
                            '<div class="details">' +
                            '<p><span>合同编码:</span><span class="sp goods_customerContractNumber">' + data[i].customerContractNumber + '</span></p>' +
                            '<p><span>客户:</span><span class="sp goods_customerNamee">' + data[i].customerName + '</span></p>' +
                            '<p><span>创建时间:</span><span class="sp goods_createTime">' + data[i].createTime + '</span></p>';
                        if (data[i].comment) {
                            data_html += '<p><span>备注:</span><span class="sp comment">' + data[i].comment + '</span></p>';
                        }
                        data_html += '</div></div></div></li>';
                    }
                    $("#work_list").append(data_html);
                    //改变 共计 数
                    $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
                } else {
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    $alert(res.message);
                }
            },
            error: function (error) {
                //隐藏提示符 显示
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                if (error.status == 0) {
                    $alert("请求超时，请再试一次！");
                } else {
                    console.log(error);
                    $alert(error.responseText);
                }
            }
        });
    }

    //点击 发货单箭头
    $(".content #work_list").on("click", "li .li_title", function () {
        var id = $(this).parent().find("span.goods_id").html();
        var billNo = $(this).parent().find("span.billNo").html();
        loading_show("选择中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + "/app/deliver/selectWKSaleoutorder",
            data: {
                _id: id
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                loading_hide();
                if (res.success) {
                    // var data = res.result;
                    var work = {
                        id: id,
                        billNo: billNo,
                    };
                    localStorage.setItem("workWK", JSON.stringify(work));
                    window.location.href = "deliverWK_one.html";
                } else {
                    //隐藏提示符 显示
                    $alert(res.message);
                }
            },
            error: function (error) {
                loading_hide();
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
    });

});